package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.OrganicGoodsAdapter;
import com.hemaapp.yjnh.bean.Blog;

/**有机商品
 * @author GuiyouZhang
 * Date：2015年12月17日 上午11:38:05
 * Email:guiyouzhang@163.com
 */
public class OrganicActivity extends BaseActivity implements OnClickListener {

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;
	private ImageView titleSearch;
	private XtomRefreshLoadmoreLayout layoutRefresh;
	private XtomListView listview;
	private LinearLayout ll_return_top;
	
	private View headerView;
	private TextView tvSlgs;
	private TextView tvTea;
	private TextView tvMmly;
	private TextView tvSchx;
	private TextView tvSzgh;
	private TextView tvQdrl;
	private TextView tvZbys;
	
	private int page = 0;
	private ArrayList<Blog> blogs = new ArrayList<Blog>();
	private OrganicGoodsAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_organic);
		super.onCreate(savedInstanceState);
		init();
	}

	private void init() {
		headerView = LayoutInflater.from(mContext).inflate(R.layout.header_organic, null);
		findHeaderViews();
		listview.addHeaderView(headerView);
        getNetWorker().blogList("2", "无", "1", "1", "", "", "1", "", "", "", "", "", "", "",page+"");
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case BLOG_LIST:
			showProgressDialog("请稍候");
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case BLOG_LIST:
			cancelProgressDialog();
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case BLOG_LIST:
			String page = netTask.getParams().get("page");
			HemaPageArrayResult<Blog> bResult = (HemaPageArrayResult<Blog>) baseResult;
			if (bResult == null || bResult.getObjects() == null)
				return;
			ArrayList<Blog> goods = bResult.getObjects();
			if ("0".equals(page)) {// 刷新
				layoutRefresh.refreshSuccess();
				layoutRefresh.setLoadmoreable(true);
				blogs.clear();
				blogs.addAll(goods);
			}else {//加载更多
				layoutRefresh.loadmoreSuccess();
				if (goods.size()>0) {
					blogs.addAll(goods);
				}else {
					layoutRefresh.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			refreshDatas(-1);
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case BLOG_LIST:
			if (page == 0) {// 刷新
				layoutRefresh.refreshFailed();
			}else {
				layoutRefresh.loadmoreFailed();
			}
			XtomToastUtil.showShortToast(mContext, "获取信息失败，"+baseResult.getMsg());
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case BLOG_LIST:
			if (page == 0) {// 刷新
				layoutRefresh.refreshFailed();
			}else {
				layoutRefresh.loadmoreFailed();
			}
			XtomToastUtil.showShortToast(mContext, "获取信息失败，请稍后再试");
            refreshDatas(failedType);
			break;
		}
	}
	
	private void refreshDatas(int failedType) {
		if (null == adapter) {
			adapter = new OrganicGoodsAdapter(mContext, blogs);
			adapter.setEmptyString("暂无商品");
			listview.setAdapter(adapter);
		}else {
			adapter.notifyDataSetChanged();
		}
		adapter.setFailtype(failedType);
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);
		titleSearch = (ImageView) findViewById(R.id.title_right_img);
		
		layoutRefresh = (XtomRefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		listview = (XtomListView)findViewById( R.id.listview );
		ll_return_top = (LinearLayout) findViewById(R.id.ll_return_top);//回到顶部
	}

	private void findHeaderViews() {
		tvSlgs = (TextView) headerView.findViewById( R.id.tv_slgs );
		tvTea = (TextView) headerView.findViewById( R.id.tv_tea );
		tvMmly = (TextView) headerView.findViewById( R.id.tv_mmly );
		tvSchx = (TextView) headerView.findViewById( R.id.tv_schx );
		tvSzgh = (TextView) headerView.findViewById( R.id.tv_szgh );
		tvQdrl = (TextView) headerView.findViewById( R.id.tv_qdrl );
		tvZbys = (TextView) headerView.findViewById( R.id.tv_zbys );

		titleText.setText("有机");
		titleLeft.setOnClickListener(this);
		titleRight.setVisibility(View.GONE);
		titleSearch.setVisibility(View.VISIBLE);
		titleSearch.setOnClickListener(this);
		tvSlgs.setOnClickListener(tabClikListener);
		tvTea.setOnClickListener(tabClikListener);
		tvMmly.setOnClickListener(tabClikListener);
		tvSchx.setOnClickListener(tabClikListener);
		tvSzgh.setOnClickListener(tabClikListener);
		tvQdrl.setOnClickListener(tabClikListener);
		tvZbys.setOnClickListener(tabClikListener);
	}

	@Override
	protected void setListener() {
		ll_return_top.setOnClickListener(this);
		layoutRefresh.setOnStartListener(new OnStartListener() {
            @Override
            public void onStartRefresh(XtomRefreshLoadmoreLayout xtomRefreshLoadmoreLayout) {
                page = 0;
                getNetWorker().blogList("2", "无", "1", "1", "", "", "1", "", "", "", "", "", "", "", page+"");
            }

            @Override
            public void onStartLoadmore(XtomRefreshLoadmoreLayout xtomRefreshLoadmoreLayout) {
                page++;
                getNetWorker().blogList("2", "无", "1", "1", "", "", "1", "", "", "", "", "", "", "", page+"");
            }
        });
		layoutRefresh.setBackgroundColor(0xfff0f0f0);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_btn:
			finish();
			break;
		case R.id.title_right_img:
			Intent itSearch = new Intent(mContext, SearchActivity.class);
			itSearch.putExtra("type", 2);
			itSearch.putExtra("assort", "有机");
			startActivity(itSearch);
			break;
		case R.id.ll_return_top:
			listview.setSelection(0);
			break;
		}
	}
	
	private OnClickListener tabClikListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
            Intent itGoods = new Intent(mContext, CmnProductionListActivity.class);
			switch (v.getId()) {
			case R.id.tv_slgs:
                itGoods.putExtra("title", "时令果蔬");
                itGoods.putExtra("keyid", "时令果蔬");
				break;
			case R.id.tv_tea:
                itGoods.putExtra("title", "茶品");
                itGoods.putExtra("keyid", "茶品");
				break;
			case R.id.tv_mmly:
                itGoods.putExtra("title", "米面粮油");
                itGoods.putExtra("keyid", "米面粮油");
				break;
			case R.id.tv_schx:
                itGoods.putExtra("title", "水产海鲜");
                itGoods.putExtra("keyid", "水产海鲜");
				break;
			case R.id.tv_szgh:
                itGoods.putExtra("title", "山珍干货");
                itGoods.putExtra("keyid", "山珍干货");
				break;
			case R.id.tv_qdrl:
                itGoods.putExtra("title", "禽蛋肉类");
                itGoods.putExtra("keyid", "禽蛋肉类");
				break;
			case R.id.tv_zbys:
                itGoods.putExtra("title", "滋补养生");
                itGoods.putExtra("keyid", "滋补养生");
				break;
			}
            itGoods.putExtra("keytype", "2");
            itGoods.putExtra("keyword", "");
            itGoods.putExtra("saleflag", "");
            itGoods.putExtra("topflag", "");
            itGoods.putExtra("cheapflag", "");
            itGoods.putExtra("score_percent", "");
            itGoods.putExtra("seller_id", "");
            itGoods.putExtra("district_name", "");
			startActivity(itGoods);
		}
	};

}
